package Buildings.dwelling.hotel;

import Buildings.dwelling.*;
import Interfaces.*;

public class Hotel extends Dwelling {

    private static double coeff(int star) {
        double coeff = -1;
        if (star < 0 || star > 5) {
            return -1;
        }
        double[] coeffmass=new double[]{0.25,0.5,1,1.25,1.5};
        return coeffmass[star-1];
    }
    public int getHotelStar() {
        int MaxStar = 1;
        HotelFloor hf = new HotelFloor(1);//экземпляр для сравнения
        for (int i = 0; i < getHowMuchFloorInBuilding(); i++) {
            if (getFloor(i).getClass() == hf.getClass()) {//проверка на то что этаж принадлежит отелю
                if (MaxStar < ((HotelFloor) getFloor(i)).getHowMuchStarInFloor()) {
                    MaxStar = ((HotelFloor) getFloor(i)).getHowMuchStarInFloor();
                }
            }
        }
        return MaxStar;
    }
    @Override
    public Space getBestSpace() {
        Space best = null;
        double max = 0;
        int n = getHowMuchFloorInBuilding();
        HotelFloor hf = new HotelFloor(1);
        for (int i = 0; i < n; i++) {
            if (getFloor(i).getClass() == hf.getClass()) {
                int m = getFloor(i).getHowMuchSpaceInFloor();
                int star = ((HotelFloor) getFloor(i)).getHowMuchStarInFloor();
                for (int j = 0; j < m; j++) {
                    if (max < (getFloor(i).getSpaceInFloor(j).getHowMuchArea() * coeff(star))) {
                        max = (getFloor(i).getSpaceInFloor(j).getHowMuchArea() * coeff(star));
                        best = getFloor(i).getSpaceInFloor(j);
                    }
                }
            }
        }
        return best;
    }
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(("Отель \n"));
        for (int i = 0; i < getHowMuchFloorInBuilding(); i++) {
            s.append((i + getFloor(i).toString()));
        }
        return s.toString();
    }
    @Override
    public boolean equals(Object o) {
        boolean answer = false;
        if (o.getClass() == this.getClass()) {
            if (((Hotel) o).getHowMuchFloorInBuilding() == this.getHowMuchFloorInBuilding() && ((Hotel) o).getHotelStar() == this.getHotelStar()) {
                boolean key = true;
                for (int i = 0; i < getHowMuchFloorInBuilding(); i++) {
                    if (this.getFloor(i).equals(((Hotel) o).getFloor(i)) == false) {
                        key = false;
                        break;
                    }
                }
                if (key == true) {
                    answer = true;
                }
            }
        }
        return answer;
    }
    @Override
    public int hashCode() {
        int hash;
        hash = (int) (Integer.bitCount(this.getHowMuchFloorInBuilding()));
        for (int i = 0; i < this.getHowMuchFloorInBuilding(); i++) {
            hash = (int) (hash ^ this.getFloor(i).hashCode());
        }
        return hash;
    }
    @Override
    public Object clone() {
        Floor forClone[]=new Floor[getHowMuchFloorInBuilding()];
        for(int i=0;i<getHowMuchFloorInBuilding();i++){
            forClone[i]=(Floor)getFloor(i).clone();
        }
        return new Hotel(forClone);
    }

    public Hotel(int howMuchFloor, int ... SpaceInFloors) {
        super(howMuchFloor, SpaceInFloors);
        for (int i = 0; i < howMuchFloor; i++) {
            setFloor(i, new HotelFloor(getFloor(i).getMassSpacesInFloor()));
        }
    }
    public Hotel(Floor ... massOfFloor) {
        super(massOfFloor);
    }
}
